/**
 * Project Name:gc.sms.mvn
 * File Name:MailUtils.java
 * Package Name:com.gc.util.email
 * Date:2018年11月7日下午1:35:39
 * Copyright (c) 2018, longbiosphere@163.com All Rights Reserved.
 *
 */
package com.clps.dev.sms.util.email;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ClassName: MailUtils <br/>
 * Description:  <br/>
 * <br/>
 * date: 2018年11月7日 下午1:35:39 <br/>
 *
 * @author long.ma
 * @version V1.0
 * @since JDK 1.8
 */

// @PropertySource(value=
// {"/gc.sms.mvn/src/main/java/com/clps/dev/sms/util/email/mail.properties"})
public class MailUtil {

	// @Value("${mail.smtp.host}")
	private String host;
	// @Value ("${mail.smtp.port}")
	private String port;
	// @Value ("${mail.smtp.SSLport}")
	private String SSLport;
	// @Value ("${mail.smtp.starttlsEnable}")
	private String starttlsEnable;
	// @Value("${mail.smtp.auth}")
	private String auth;
	// @Value ("${mail.smtp.connectiontimeout}")
	private String connectiontimeout;
	// @Value ("${mail.smtp.account}")
	private String account;
	// @Value ("${mail.smtp.password}")
	private String password;
	// @Value ("${mail.smtp.timeout}")
	private String timeout;

	private String nickname = "duan";// 发送者,显示的发件人名字

	private Properties properties;
	private Session session;
	private Transport transport;
	private MimeMessage mimeMessage;

	// 用于日志输出的全局变量
	public static Logger log = (Logger) LoggerFactory.getLogger(MailUtil.class);

	/*
public static void main(String[] args) throws UnsupportedEncodingException {

		MailUtil mailUtil;
		try {
			mailUtil = new MailUtil();
			mailUtil.sendMail("281800151@qq.com", "1083891667@qq.com", "", "", "测试", "测试MailUtils类");
		} catch (IOException e1) {
			
			e1.printStackTrace();
		} catch (MessagingException e) {
			
			e.printStackTrace();
		}
	}
*/
	/**
	 * 发送邮件
	 * 
	 * @param emailFrom
	 *            发件人
	 * @param emailTo
	 *            收件人
	 * @param emailCC
	 *            抄送
	 * @param subject
	 *            主题
	 * @param content
	 *            内容
	 * @return
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 */

	public boolean sendMail(final String emailFrom, String emailTo, String emailCC, String emailBCC, String subject,
			String content) throws MessagingException, UnsupportedEncodingException {

		boolean result = true;

		transport = null;
		session = Session.getDefaultInstance(properties, null);
		transport = session.getTransport("smtp");
		transport.connect(host, account, password);

		mimeMessage = new MimeMessage(session);
		mimeMessage.setSentDate(new Date());

		// 设置发送者
		InternetAddress fromAddress = new InternetAddress(account, nickname);
		mimeMessage.setFrom(fromAddress);

		// 设置发送方式与接收者
		if (!emailTo.equals("") || emailCC == null) {
			functionTO(emailTo);
		}

		// 设置发送方式与抄送者
		if (!emailCC.equals("") || emailCC == null) {
			functionCC(emailCC);
		}

		// 设置发送方式与密抄者
		if (!emailBCC.equals("") || emailBCC == null) {
			functionBCC(emailBCC);
		}

		// 设置邮件主题
		mimeMessage.setSubject(subject, "UTF-8");
		// 设置正文
		mimeMessage.setContent(content, "text/html; charset=utf-8");

		// 发送邮件
		transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
		log.info("邮件发送成功。");
		return result;
	}

	/**
	 * 
	 * functionTO:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 *
	 * @author long.ma
	 * @param emailTo
	 * @throws AddressException
	 * @throws MessagingException
	 * @since JDK 1.8
	 */
	public void functionTO(String emailTo) throws AddressException, MessagingException {

		mimeMessage.setRecipient(RecipientType.TO, new InternetAddress(emailTo));

	}

	/**
	 * 
	 * functionCC:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 *
	 * @author long.ma
	 * @param emailCC
	 * @throws AddressException
	 * @throws MessagingException
	 * @since JDK 1.8
	 */
	public void functionCC(String emailCC) throws AddressException, MessagingException {
		mimeMessage.setRecipient(RecipientType.CC, new InternetAddress(emailCC));
	}

	/**
	 * 
	 * functionBCC:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 *
	 * @author long.ma
	 * @param emailBCC
	 * @throws AddressException
	 * @throws MessagingException
	 * @since JDK 1.8
	 */
	public void functionBCC(String emailBCC) throws AddressException, MessagingException {
		mimeMessage.setRecipient(RecipientType.BCC, new InternetAddress(emailBCC));
	}

	/**
	 * Creates a new instance of MailUtil.
	 * 
	 * @throws IOException
	 *
	 */

	public MailUtil() throws IOException {

		log.info("开始发送邮件。");

		InputStream in =MailUtil.class.getResourceAsStream("mail.properties");
		Properties pps = new Properties();
		pps.load(in);
		in.close();

		this.host = pps.getProperty("mail.smtp.host");
		this.port=pps.getProperty("mail.smtp.port");
		this.SSLport=pps.getProperty("mail.smtp.SSLport");
		this.starttlsEnable=pps.getProperty("mail.smtp.starttlsEnable");
		this.auth=pps.getProperty("mail.smtp.auth");
		this.connectiontimeout=pps.getProperty("mail.smtp.connectiontimeout");
		this.account=pps.getProperty("mail.smtp.account");
		this.password=pps.getProperty("mail.smtp.password");
		this.timeout=pps.getProperty("mail.smtp.timeout");
		
		
		

		properties = System.getProperties();

		properties.put("mail.smtp.host", host);
		// 指定协议
		properties.put("mail.transport.protocol", "smtp");
		properties.put("mail.smtp.port", SSLport);
		properties.put("mail.smtp.starttls.enable", starttlsEnable);

		properties.put("mail.smtp.auth", auth);
		properties.put("mail.smtp.connectiontimeout", connectiontimeout);
		properties.put("mail.smtp.timeout", timeout);
		properties.put("mail.smtp.debug", true);

	}

	/**
	 * host.
	 *
	 * @return the host
	 * @since JDK 1.8
	 */
	public final String getHost() {
		return host;
	}

	/**
	 * host.
	 *
	 * @param host
	 *            the host to set
	 * @since JDK 1.8
	 */
	public final void setHost(String host) {
		this.host = host;
	}

	/**
	 * port.
	 *
	 * @return the port
	 * @since JDK 1.8
	 */
	public final String getPort() {
		return port;
	}

	/**
	 * port.
	 *
	 * @param port
	 *            the port to set
	 * @since JDK 1.8
	 */
	public final void setPort(String port) {
		this.port = port;
	}

	/**
	 * sSLport.
	 *
	 * @return the sSLport
	 * @since JDK 1.8
	 */
	public final String getSSLport() {
		return SSLport;
	}

	/**
	 * sSLport.
	 *
	 * @param sSLport
	 *            the sSLport to set
	 * @since JDK 1.8
	 */
	public final void setSSLport(String sSLport) {
		SSLport = sSLport;
	}

	/**
	 * starttlsEnable.
	 *
	 * @return the starttlsEnable
	 * @since JDK 1.8
	 */
	public final String getStarttlsEnable() {
		return starttlsEnable;
	}

	/**
	 * starttlsEnable.
	 *
	 * @param starttlsEnable
	 *            the starttlsEnable to set
	 * @since JDK 1.8
	 */
	public final void setStarttlsEnable(String starttlsEnable) {
		this.starttlsEnable = starttlsEnable;
	}

	/**
	 * auth.
	 *
	 * @return the auth
	 * @since JDK 1.8
	 */
	public final String getAuth() {
		return auth;
	}

	/**
	 * auth.
	 *
	 * @param auth
	 *            the auth to set
	 * @since JDK 1.8
	 */
	public final void setAuth(String auth) {
		this.auth = auth;
	}

	/**
	 * connectiontimeout.
	 *
	 * @return the connectiontimeout
	 * @since JDK 1.8
	 */
	public final String getConnectiontimeout() {
		return connectiontimeout;
	}

	/**
	 * connectiontimeout.
	 *
	 * @param connectiontimeout
	 *            the connectiontimeout to set
	 * @since JDK 1.8
	 */
	public final void setConnectiontimeout(String connectiontimeout) {
		this.connectiontimeout = connectiontimeout;
	}

	/**
	 * account.
	 *
	 * @return the account
	 * @since JDK 1.8
	 */
	public final String getAccount() {
		return account;
	}

	/**
	 * account.
	 *
	 * @param account
	 *            the account to set
	 * @since JDK 1.8
	 */
	public final void setAccount(String account) {
		this.account = account;
	}

	/**
	 * password.
	 *
	 * @return the password
	 * @since JDK 1.8
	 */
	public final String getPassword() {
		return password;
	}

	/**
	 * password.
	 *
	 * @param password
	 *            the password to set
	 * @since JDK 1.8
	 */
	public final void setPassword(String password) {
		this.password = password;
	}

	/**
	 * timeout.
	 *
	 * @return the timeout
	 * @since JDK 1.8
	 */
	public final String getTimeout() {
		return timeout;
	}

	/**
	 * timeout.
	 *
	 * @param timeout
	 *            the timeout to set
	 * @since JDK 1.8
	 */
	public final void setTimeout(String timeout) {
		this.timeout = timeout;
	}

	/**
	 * nickname.
	 *
	 * @return the nickname
	 * @since JDK 1.8
	 */
	public final String getNickname() {
		return nickname;
	}

	/**
	 * nickname.
	 *
	 * @param nickname
	 *            the nickname to set
	 * @since JDK 1.8
	 */
	public final void setNickname(String nickname) {
		this.nickname = nickname;
	}

	/**
	 * log.
	 *
	 * @return the log
	 * @since JDK 1.8
	 */
	public static final Logger getLog() {
		return log;
	}

	/**
	 * log.
	 *
	 * @param log
	 *            the log to set
	 * @since JDK 1.8
	 */
	public static final void setLog(Logger log) {
		MailUtil.log = log;
	}

}
